Imports System
Imports System.Windows.Forms

<ToolboxBitmap(GetType(ToolTip))> _
Public Class ACCMSSeleccionar
   Inherits ContextMenuStrip

   Friend WithEvents cmsMarcarTodos As System.Windows.Forms.ToolStripMenuItem
   Friend WithEvents cmsMarcarSeleccionados As System.Windows.Forms.ToolStripMenuItem
   Friend WithEvents cmsDesmarcarTodos As System.Windows.Forms.ToolStripMenuItem
   Friend WithEvents cmsDesmarcarSeleccionados As System.Windows.Forms.ToolStripMenuItem
   Public Event ACClick(ByVal sender As ACSeleccionar, ByVal e As EventArgs)

   Public Sub New()
      MyBase.New()

      '
      'cmsMarcarTodos
      '
      Me.cmsMarcarTodos = New System.Windows.Forms.ToolStripMenuItem
      Me.cmsMarcarTodos.Name = "cmsMarcarTodos"
      Me.cmsMarcarTodos.Size = New System.Drawing.Size(206, 22)
      Me.cmsMarcarTodos.Text = "Marcar Todos"
      '
      'cmsMarcarSeleccionados
      '
      Me.cmsMarcarSeleccionados = New System.Windows.Forms.ToolStripMenuItem
      Me.cmsMarcarSeleccionados.Name = "cmsMarcarSeleccionados"
      Me.cmsMarcarSeleccionados.Size = New System.Drawing.Size(206, 22)
      Me.cmsMarcarSeleccionados.Text = "Marcar Seleccionados"
      '
      'cmsDesmarcarTodos
      '
      Me.cmsDesmarcarTodos = New System.Windows.Forms.ToolStripMenuItem
      Me.cmsDesmarcarTodos.Name = "cmsDesmarcarTodos"
      Me.cmsDesmarcarTodos.Size = New System.Drawing.Size(206, 22)
      Me.cmsDesmarcarTodos.Text = "Desmarcar Todos"
      '
      'cmsDesmarcarSeleccionados
      '
      Me.cmsDesmarcarSeleccionados = New System.Windows.Forms.ToolStripMenuItem
      Me.cmsDesmarcarSeleccionados.Name = "cmsDesmarcarSeleccionados"
      Me.cmsDesmarcarSeleccionados.Size = New System.Drawing.Size(206, 22)
      Me.cmsDesmarcarSeleccionados.Text = "Desmarcar Seleccionados"
      '
      'cmsOpciones
      '
      Me.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.cmsMarcarTodos, Me.cmsMarcarSeleccionados, Me.cmsDesmarcarTodos, Me.cmsDesmarcarSeleccionados})
      Me.Name = "cmsOpciones"
      Me.Size = New System.Drawing.Size(207, 92)
   End Sub

   Private m_grilla As Object
   Private m_columna As String

   Public Sub ACMostrar(ByRef x_grilla As Object, ByVal x_posicion As Point, Optional ByVal x_columna As String = "Seleccionar")
      Try
         m_grilla = x_grilla
         m_columna = x_columna
         Me.Show(x_posicion)
      Catch ex As Exception

      End Try
   End Sub

   Private Sub cmsMarcarTodos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmsMarcarTodos.Click
      Seleccionar(ACSeleccionar.MarcarTodos)
      RaiseEvent ACClick(ACSeleccionar.MarcarTodos, e)
   End Sub
   Private Sub cmsMarcarSeleccionados_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmsMarcarSeleccionados.Click
      Seleccionar(ACSeleccionar.MarcarSeleccionados)
      RaiseEvent ACClick(ACSeleccionar.MarcarSeleccionados, e)
   End Sub
   Private Sub cmsDesmarcarTodos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmsDesmarcarTodos.Click
      Seleccionar(ACSeleccionar.DesmarcarTodos)
      RaiseEvent ACClick(ACSeleccionar.DesmarcarTodos, e)
   End Sub
   Private Sub cmsDesmarcarSeleccionados_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmsDesmarcarSeleccionados.Click
      Seleccionar(ACSeleccionar.DesmarcarSeleccionados)
      RaiseEvent ACClick(ACSeleccionar.DesmarcarSeleccionados, e)
   End Sub

   Public Enum ACSeleccionar
      MarcarTodos
      MarcarSeleccionados
      DesmarcarTodos
      DesmarcarSeleccionados
   End Enum
   Private Sub Seleccionar(ByVal x_seleccion As ACSeleccionar)
      Select Case m_grilla.GetType().ToString()
         Case "System.Windows.Forms.DataGridView"
            SeleccionarDataGridView(x_seleccion, m_grilla, m_columna)
      End Select
   End Sub
   Private Sub SeleccionarDataGridView(ByVal x_seleccion As ACSeleccionar, ByRef x_dataGridView As DataGridView, Optional ByVal x_columna As String = "Seleccionar")
      Select Case x_seleccion
         Case ACSeleccionar.MarcarTodos
            For Each datagridrow As DataGridViewRow In x_dataGridView.Rows
               datagridrow.Cells(x_columna).Value = True
            Next
         Case ACSeleccionar.MarcarSeleccionados
            For Each datagridrow As DataGridViewRow In x_dataGridView.SelectedRows
               datagridrow.Cells(x_columna).Value = True
            Next
         Case ACSeleccionar.DesmarcarTodos
            For Each datagridrow As DataGridViewRow In x_dataGridView.Rows
               datagridrow.Cells(x_columna).Value = False
            Next
         Case ACSeleccionar.DesmarcarSeleccionados
            For Each datagridrow As DataGridViewRow In x_dataGridView.SelectedRows
               datagridrow.Cells(x_columna).Value = False
            Next
      End Select
   End Sub

End Class